using Microsoft.AspNetCore.Identity;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;
using TrackSystem.API.Extensions;
using TrackSystem.API.SignalRHub;
using TrackSystem.Entities.Models;
using TrackSystem.EntityFramework;

namespace TrackSystem.API
{
    /// <summary>
    /// 主程序
    /// </summary>
    public class Program
    {
        /// <summary>
        /// 主程序入口
        /// </summary>
        /// <param name="args"></param>
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            builder.Services.AddEmailService(builder.Configuration.GetSection("FromEmailConfig"));

            // Add services to the container
            builder.Services.AddControllers().AddJsonOptions(options => {
                options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
            });

            builder.Services.AddSignalR();

            // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
            builder.Services.AddEndpointsApiExplorer();
            builder.Services.ConfigureSwagger();
            builder.Services.AddAutoMapper(Assembly.GetExecutingAssembly());

            builder.Services.ConfigureCors();
            builder.Services.ConfigurationMySqlContext(builder.Configuration);
            builder.Services.ConfigureRepositoryWrapper();


            var app = builder.Build();

            // Configure the HTTP request pipeline.
            if (app.Environment.IsDevelopment())
            {
                app.UseSwagger();
                app.UseSwaggerUI();
            }            

            // 跨域保护解除
            app.UseCors("CorsPolicy");

            // 使用认证和授权
            app.UseAuthentication();
            app.UseAuthorization();


            app.MapControllers();

            app.MapHub<TrackHub>("/trackhub");
            app.MapHub<LogicHelperHub>("/logichub");
            app.MapHub<LoopHelperHub>("/loophub");
            app.MapHub<SlaveHelperHub>("/slavehub");           
            app.MapHub<IOControlHub>("/ioctlhub");

            app.Run();
        }
    }
}
